from typing import *


class Solution:

    def largestValsFromLabels(self, values: List[int], labels: List[int],
                              numWanted: int, useLimit: int) -> int:
        nums = sorted(zip(values, labels), reverse=True)
        h = DefaultDict(int)
        ans = cnt = 0
        for v, l in nums:
            if h[l] < useLimit:
                ans += v
                h[l] += 1
                cnt += 1
                if cnt == numWanted:
                    break
        return ans